﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CommonClasses
{
    public class PokerGame : Game
    {
        public delegate void PokerWinnerEventHandler(string name, List<int> total);
        public event PokerWinnerEventHandler PokerWinner;

        public delegate void NotEnoughCardsEventHandler();
        public event NotEnoughCardsEventHandler NotEnoughCards;        

        public Deck myPokerDeck;
        public PokerHand playerHand;
        public PokerHand dealerHand;

        public override void NewGame()
        {
            myPokerDeck = new Deck();
            myPokerDeck.initialize_cards();
        }

        public override void Deal()
        {            
            if (myPokerDeck.Cards.Count < 13)
            {
                if (NotEnoughCards != null) NotEnoughCards();
                return;
            }

            playerHand = new PokerHand();
            dealerHand = new PokerHand();           

            playerHand.name = "You";
            dealerHand.name = "Dealer";

            for (int i = 0; i < 5; i++)
            {
                playerHand.CardsOnHand.Add(myPokerDeck.GetCard());
                playerHand.CardsOnHand[i].FaceDown = false; 
                dealerHand.CardsOnHand.Add(myPokerDeck.GetCard());
            }

            playerHand.calculate_total();
        }

        public void DealerFaceUp()
        {
            // Show Dealer's cards.             
            foreach (Card c in dealerHand.CardsOnHand)
            {
                c.FaceDown = false;
            }
        }

        public override void Evaluate()
        {        
            // Calculate total for both player and dealer.
            playerHand.calculate_total();
            dealerHand.calculate_total();

            // Determine the shorter List<int> since that will be the end index of the total comparison loop. 
            int shorter_list = playerHand.Total.Count;
            if (shorter_list > dealerHand.Total.Count)
            {
                shorter_list = dealerHand.Total.Count;
            }

            // Compare the 1st element of the playerHand.Total and dealerHand.Total to determine winner.
            // If 1st element is equal (both player and dealer have the same cards), proceed to the 
            // next element to nth element to determine which card is higher.
            bool isTie = true;
            for (int i = 0; i < shorter_list; i++)
            {
                if (playerHand.Total[i] == dealerHand.Total[i])
                {
                    continue;
                }
                else
                {
                    isTie = false;
                    if (playerHand.Total[i] > dealerHand.Total[i])
                    {                        
                        if (PokerWinner != null) PokerWinner(playerHand.name, playerHand.Total);
                    }
                    else
                    {
                        if (PokerWinner != null) PokerWinner(dealerHand.name, dealerHand.Total);
                    }
                    break;
                }
            }

            if (isTie)
            {
                if (PokerWinner != null) PokerWinner("Tie", dealerHand.Total);
            }
        }
    
        //public override void PlayerMove()
        //{
        //    throw new NotImplementedException();
        //}
        
        public void PlayerMove(List<int> cardsToRemove)
        {
            cardsToRemove.Sort();
            cardsToRemove.Reverse();

            for (int i = 0; i < cardsToRemove.Count; i++)
            {
                playerHand.CardsOnHand.RemoveAt(cardsToRemove[i]);
            }

            if (playerHand.CardsOnHand.Count() != playerHand.MaxCards)
            {
                int diff = playerHand.MaxCards - playerHand.CardsOnHand.Count;

                for (int j = 0; j < diff; j++)
                {
                    playerHand.CardsOnHand.Add(myPokerDeck.GetCard());
                    playerHand.CardsOnHand[playerHand.CardsOnHand.Count - 1].FaceDown = false;
                }                         
            }
        }
    }
}

